home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Comm / www / tidy_os4.lha / tidy / src / fileio.c < prev    next >
C/C++ Source or Header  |  2004-07-25  |  2KB  |  90 lines

  1. /* fileio.c -- does standard I/O
  2.  
  3.   (c) 1998-2004 (W3C) MIT, ERCIM, Keio University
  4.   See tidy.h for the copyright notice.
  5.  
  6.   CVS Info :
  7.  
  8.     $Author: terry_teague $ 
  9.     $Date: 2004/02/29 03:55:22 $ 
  10.     $Revision: 1.6 $ 
  11.  
  12.   Default implementations of Tidy input sources
  13.   and output sinks based on standard C FILE*.
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18.  
  19. #include "fileio.h"
  20. #include "tidy.h"
  21.  
  22.  
  23. typedef struct _fp_input_source
  24. {
  25.     FILE*        fp;
  26.     TidyBuffer   unget;
  27. } FileSource;
  28.  
  29. static int filesrc_getByte( ulong sourceData )
  30. {
  31.   FileSource* fin = (FileSource*) sourceData;
  32.   int bv;
  33.   if ( fin->unget.size > 0 )
  34.     bv = tidyBufPopByte( &fin->unget );
  35.   else
  36.     bv = fgetc( fin->fp );
  37.   return bv;
  38. }
  39.  
  40. static Bool filesrc_eof( ulong sourceData )
  41. {
  42.   FileSource* fin = (FileSource*) sourceData;
  43.   Bool isEOF = ( fin->unget.size == 0 );
  44.   if ( isEOF )
  45.     isEOF = feof( fin->fp );
  46.   return isEOF;
  47. }
  48.  
  49. static void filesrc_ungetByte( ulong sourceData, byte bv )
  50. {
  51.   FileSource* fin = (FileSource*) sourceData;
  52.   tidyBufPutByte( &fin->unget, bv );
  53. }
  54.  
  55. void initFileSource( TidyInputSource* inp, FILE* fp )
  56. {
  57.   FileSource* fin = NULL;
  58.  
  59.   inp->getByte    = filesrc_getByte;
  60.   inp->eof        = filesrc_eof;
  61.   inp->ungetByte  = filesrc_ungetByte;
  62.  
  63.   fin = (FileSource*) MemAlloc( sizeof(FileSource) );
  64.   ClearMemory( fin, sizeof(FileSource) );
  65.   fin->fp = fp;
  66.   inp->sourceData = (ulong) fin;
  67. }
  68.  
  69. void freeFileSource( TidyInputSource* inp, Bool closeIt )
  70. {
  71.     FileSource* fin = (FileSource*) inp->sourceData;
  72.     if ( closeIt && fin && fin->fp )
  73.       fclose( fin->fp );
  74.     tidyBufFree( &fin->unget );
  75.     MemFree( fin );
  76. }
  77.  
  78. void filesink_putByte( ulong sinkData, byte bv )
  79. {
  80.   FILE* fout = (FILE*) sinkData;
  81.   fputc( bv, fout );
  82. }
  83.  
  84. void  initFileSink( TidyOutputSink* outp, FILE* fp )
  85. {
  86.   outp->putByte  = filesink_putByte;
  87.   outp->sinkData = (ulong) fp;
  88. }
  89.  
  90.